package com.duolaibei.business.view.activity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.api.ApiName;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.config.Preferences;
import com.duolaibei.business.http.HttpUtils;
import com.duolaibei.business.model.ResponseResult;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.GsonUtil;
import com.duolaibei.business.utils.PicassoUtils;
import com.duolaibei.business.utils.PreferenceUtil;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.view.dialog.SavePhotoDialog;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by xlm on 2017/11/8 0008
 */

public class QrcodeActivity extends BaseActivity {

    @Inject(R.id.back)
    private RelativeLayout mLayoutBack;
    @Inject(R.id.title)
    private TextView mViewTitle;
    @Inject(R.id.collect_qrcode)
    private ImageView mImageViewQrcode;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_qrcode);
    }

    @Override
    protected void initViews() {
        mViewTitle.setText("收款码");
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getQrcode();
    }

    @Override
    protected void onSetListener() {
        mLayoutBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mImageViewQrcode.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                showDoalog();
                return true;
            }
        });
    }

    private SavePhotoDialog mSavePhotoDialog;

    /**
     * 显示保存弹窗
     */
    private void showDoalog() {
        if (mSavePhotoDialog == null) {
            mSavePhotoDialog = new SavePhotoDialog(getThis());
        }
        mSavePhotoDialog.setCanceledOnTouchOutside(true);
        mSavePhotoDialog.setListener(new SavePhotoDialog.Listener() {
            @Override
            public void onComfirm() {
                super.onComfirm();
                mSavePhotoDialog.dismiss();
                saveToPhoto();
            }

            @Override
            public void onCancle() {
                super.onCancle();
                mSavePhotoDialog.dismiss();
            }
        });
        mSavePhotoDialog.show();
    }

    //截图并保存相册
    private void saveToPhoto() {
        if (CommonUtil.getAndSaveCurrentImage(getThis(), "qrcode")) {
            showToast("图片已保存到相册");
        }
    }

    /**
     * 获取收款码数据
     */
    private void getQrcode() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.QROCDE);
        param.add("uid", String.valueOf(PreferenceUtil.getParam(Preferences.UID, "")));
        param.add("login_type", String.valueOf(PreferenceUtil.getParam(Preferences.LOGINTYPE, "")));
        param.add("login_id", String.valueOf(PreferenceUtil.getParam(Preferences.LOGINID, "")));
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().url(ApiName.API).params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                if (CommonUtil.isForeground(getThis())) {
                    showToast(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (result.getStatus() == 1) {
                        JSONObject object = null;
                        try {
                            object = new JSONObject(string);
                            JSONObject object1 = object.getJSONObject("data");
                            final String qrocde = object1.optString("qrcode");
                            if (CommonUtil.checkURL(qrocde)) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        PicassoUtils.getinstance().LoadImage(getThis(), qrocde,
                                                mImageViewQrcode,
                                                R.mipmap.white_bg,
                                                R.mipmap.white_bg,
                                                PicassoUtils.PICASSO_BITMAP_SHOW_NORMAL_TYPE, 0);
                                    }
                                });

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        showToast(result.getInfo());
                    }
                } else {
                    showToast(string);
                }
            }
        });
    }
}
